<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset=utf-8 />
 <title>Minimal Websocket test app</title>
</head>

<body>
<h3>Detected Browser: <div id=brow>...</div></h3>
<h2>libwebsockets "dumb-increment-protocol" test applet</h2>
The incrementing number is coming from the server and is individual for
each connection to the server... try opening a second browser window.
Click the button to send the server a websocket message to
reset the number.<br><br>

<table>
	<tr>
		<td align=center><input type=button id=offset1 value="Reset counter" onclick="reset1();" ></td>
		<td width=100 align=center><div id=number1> </div></td>
		<td id=wsdi_statustd1 align=center><div id=wsdi_status1>Not initialized</div></td>
	</tr>
	<tr>
		<td align=center><input type=button id=offset2 value="Reset counter" onclick="reset2();" ></td>
		<td width=100 align=center><div id=number2> </div></td>
		<td id=wsdi_statustd2 align=center><div id=wsdi_status2>Not initialized</div></td>
	</tr>
	<tr>
		<td align=center><input type=button id=offset3 value="Reset counter" onclick="reset3();" ></td>
		<td width=100 align=center><div id=number3> </div></td>
		<td id=wsdi_statustd3 align=center><div id=wsdi_status3>Not initialized</div></td>
	</tr>	
</table>

<script>

/* BrowserDetect came from http://www.quirksmode.org/js/detect.html */

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera",
			versionSearch: "Version"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
	+ BrowserDetect.version +" " + BrowserDetect.OS +" ";

	var pos = 0;

function get_appropriate_ws_url()
{
	var pcol;
	var u = document.URL;

	/*
	 * We open the websocket encrypted if this page came on an
	 * https:// url itself, otherwise unencrypted
	 */

	if (u.substring(0, 5) == "https") {
		pcol = "wss://";
		u = u.substr(8);
	} else {
		pcol = "ws://";
		if (u.substring(0, 4) == "http")
			u = u.substr(7);
	}

	//u = u.split('/');
	u = "127.0.0.1:7681";
	return pcol + u;
}


document.getElementById("number1").textContent = get_appropriate_ws_url();
document.getElementById("number2").textContent = get_appropriate_ws_url();
document.getElementById("number3").textContent = get_appropriate_ws_url();

/* dumb increment protocol */
	
	var socket_di1;
	var socket_di2;
	var socket_di3;

	if (BrowserDetect.browser == "Firefox") {
		socket_di1 = new MozWebSocket(get_appropriate_ws_url(),
				   "dumb-increment-protocol");
		socket_di2 = new MozWebSocket(get_appropriate_ws_url(),
				   "dumb-increment-protocol");
		socket_di3 = new MozWebSocket(get_appropriate_ws_url(),
				   "dumb-increment-protocol");
	} else {
		socket_di1 = new WebSocket(get_appropriate_ws_url(),
				   "dumb-increment-protocol");
		socket_di2 = new WebSocket(get_appropriate_ws_url(),
				   "dumb-increment-protocol");
		socket_di3 = new WebSocket(get_appropriate_ws_url(),
				   "dumb-increment-protocol");
	}


	try {
		socket_di1.onopen = function() {
			document.getElementById("wsdi_statustd1").style.backgroundColor = "#40ff40";
			document.getElementById("wsdi_status1").textContent = " websocket connection opened ";
		} 

		socket_di1.onmessage =function got_packet(msg) {
			document.getElementById("number1").textContent = msg.data + "\n";
		} 

		socket_di1.onclose = function(){
			document.getElementById("wsdi_statustd1").style.backgroundColor = "#ff4040";
			document.getElementById("wsdi_status1").textContent = " websocket connection CLOSED ";
		}

		socket_di2.onopen = function() {
			document.getElementById("wsdi_statustd2").style.backgroundColor = "#40ff40";
			document.getElementById("wsdi_status2").textContent = " websocket connection opened ";
		} 

		socket_di2.onmessage =function got_packet(msg) {
			document.getElementById("number2").textContent = msg.data + "\n";
		} 

		socket_di2.onclose = function(){
			document.getElementById("wsdi_statustd2").style.backgroundColor = "#ff4040";
			document.getElementById("wsdi_status2").textContent = " websocket connection CLOSED ";
		}

		socket_di3.onopen = function() {
			document.getElementById("wsdi_statustd3").style.backgroundColor = "#40ff40";
			document.getElementById("wsdi_status3").textContent = " websocket connection opened ";
		} 

		socket_di3.onmessage =function got_packet(msg) {
			document.getElementById("number3").textContent = msg.data + "\n";
		} 

		socket_di3.onclose = function(){
			document.getElementById("wsdi_statustd3").style.backgroundColor = "#ff4040";
			document.getElementById("wsdi_status3").textContent = " websocket connection CLOSED ";
		}
	} catch(exception) {
		alert('<p>Error' + exception);  
	}

function reset1() {
	socket_di1.send("reset\n");
}

function reset2() {
	socket_di2.send("reset\n");
}

function reset3() {
	socket_di3.send("reset\n");
}


</script>

</body>
</html>
